﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace cadbom
{
    public partial class FuYuanForm : Form
    {
        FuYuanProcess process = null;
        NetApi netApi = new NetApi();
        public FuYuanForm()
        {
            netApi.InitDomainConfig();
            netApi.getUserInfo();
            process = new FuYuanProcess();
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.button1.Enabled=true;
            this.button3.Enabled = true;
            process.ExecuteLisp(new Action<String>(r =>
            {
                // 假设这是你的Form类的一个方法
                this.Invoke(new Action(() =>
                {
                    // 更新UI的代码
                    this.richTextBox1.AppendText(" 坐标点读取完成!\n");
                }));

            }));
        }

        private void button3_Click(object sender, EventArgs e)
        {
            process.UnlockAllLayersAndReadObject(new Action<List<Dictionary<String, Dictionary<String, String>>>>(r =>
            {

                   List<BasProductImport> importProductList = new List<BasProductImport>();
                   int i = 0;
                   String mainProductName = "";
                    r.ForEach(p =>
                    {
                       
                        foreach (var key in p.Keys)
                        {
                            i++;
                            //BomForm bomForm = new BomForm();
                            //bomForm.TT7SaveBomFormData(key, result);
                            BasProductImport importProduct = new BasProductImport();

                            importProduct.name = key;
                            
                            this.richTextBox1.AppendText("产品 : " + key + " \n");
                            foreach (var key1 in p[key].Keys)
                            {
                                this.richTextBox1.AppendText(key1 + ":" + p[key][key1] + " \n");
                                if (key1 == "表面处理")
                                {
                                    importProduct.description = p[key][key1];

                                }
                                if (key1 == "handleid")
                                {
                                    importProduct.cadHandleId = p[key][key1];
                                }
                                if (key1 == "客户编号")
                                {
                                    mainProductName = p[key][key1];
                                }
                                if (key1 == "项目")
                                {
                                    importProduct.importQty = p[key][key1];
                                }
                                if (key1 == "零件编号")
                                {
                                    importProduct.code = p[key][key1];
                                }
                            }
                            importProduct.customerCode = "C002";
                            importProduct.unitName = "PCS";
                            DateTime now = DateTime.Now;
                            Random random = new Random();
                            importProduct.custCode = now.ToString("yyyy-MM-dd").Replace("-", "") + random.Next(1000, 9999).ToString() + i;
                            importProduct.productVer = "A";
                            importProduct.productTypeName = "半成品";
                            importProduct.currencyName = "人民币";
                            importProduct.drawingNo = mainProductName;
                            importProductList.Add(importProduct);
                        }

                    });
                    if (importProductList.Count() > 0)
                    {
                        if (mainProductName != null)
                        {
                            BasProductImport importProduct = new BasProductImport();
                            importProduct.name = mainProductName;
                            importProduct.customerCode = "C002";
                            importProduct.unitName = "PCS";
                            DateTime now = DateTime.Now;
                            Random random = new Random();
                            importProduct.custCode =  now.ToString("yyyy-MM-dd").Replace("-","") + random.Next(1000, 9999).ToString() + 999;
                            importProduct.productVer = "A";
                            importProduct.productTypeName = "成品";
                            importProduct.currencyName = "人民币";
                            importProductList.Add(importProduct);
                        }
                        netApi.saveProductList(importProductList,new Action<List<BasProduct>>(result=>{
                                Object aa = result;
                                process.updateCadProductCode(result, new Action<string>(rr =>
                                {
                                    string aa1 = rr;
                                }));
                            }));
                    }

                //netApi.saveProcessBom(r, new Action<string>(result =>
                //{

                //    int i = 0;
                //    r.ForEach(p =>
                //    {
                //        i++;
                //        this.richTextBox1.AppendText("第" + i + "图" + " \n");
                //        foreach (var key in p.Keys)
                //        {
                //            //BomForm bomForm = new BomForm();
                //            //bomForm.TT7SaveBomFormData(key, result);


                //            this.richTextBox1.AppendText("产品 : " + key + " \n");
                //            foreach (var key1 in p[key].Keys)
                //            {
                //                this.richTextBox1.AppendText(key1 + ":" + p[key][key1] + " \n");
                //            }
                //        }


                //    });
                //}));

            }));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            process.createProcessBom(new Action<List<Dictionary<String, Dictionary<String, String>>>>(r =>
            {
                netApi.saveProcessBom(r, new Action<string>(result =>
                {

                    int i = 0;
                    r.ForEach(p =>
                    {
                        i++;
                        this.richTextBox1.AppendText("第" + i + "图" + " \n");
                        foreach (var key in p.Keys)
                        {
                            //BomForm bomForm = new BomForm();
                            //bomForm.TT7SaveBomFormData(key, result);


                            this.richTextBox1.AppendText("产品 : " + key + " \n");
                            foreach (var key1 in p[key].Keys)
                            {
                                this.richTextBox1.AppendText(key1 + ":" + p[key][key1] + " \n");
                            }
                        }


                    });
                }));

            }));
        }

        private void button4_Click(object sender, EventArgs e)
        {
            process.SelectAndFind();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            process.SelectAndCopyToNewDwg();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            process.GeneratePDFWithParams();
            
        }
    }
}
